﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;

namespace FlashSolution
{
    public class FlashEditorPart : EditorPart
    {
        private EditorPartTextboxSection section_flashvars = null;
        private EditorPartAssetUrlSelectorSection section_expressinstall = null;
        private EditorPartAssetUrlSelectorSection section_movie = null;
        private EditorPartTextboxWithBuilderSection section_althtml = null;
        private EditorPartTextboxWithBuilderSection section_minimalversion = null;

        public FlashEditorPart()
        {
            ID = "FlashEditorPart";
        }

        protected override void CreateChildControls()
        {
            this.Title = "Flash Web Part Settings";
            this.Description = "This Web Part allows for displaying a Flash movie (.swf)";

            #region "Flash Movie Settings" collapsable table
            EditorPartCollapsableTable flashmoviesettings_collapsabletable = new EditorPartCollapsableTable(this, "Expand: Flash Movie Settings", "Collapse: Flash Movie Settings", "Flash Movie Settings", "moviesectiontable", "/_layouts/Macaw.Moss2007.FlashSolution/FlashMovieSettings.htm");
            
            EditorPartTable flashmoviesettings_table = new EditorPartTable();
            flashmoviesettings_collapsabletable.control.Controls.Add(flashmoviesettings_table);

            #region "Flash Movie Url" Section
            section_movie = new EditorPartAssetUrlSelectorSection("Movie", "Flash Movie Url", "Flash Movie Url");
            section_movie.selector.UseImageAssetPicker = false;
            section_movie.selector.AssetPickerButtonVisible = true;
            section_movie.selector.PickerButtonText = "...";
            flashmoviesettings_table.Rows.Add(section_movie);            
            #endregion

            #region "Minimal FlashPlayer version requirement" Section
            section_minimalversion = new EditorPartTextboxWithBuilderSection("MinimalVersion", "Minimal FlashPlayer version requirement", "Minimal FlashPlayer version requirement");
            flashmoviesettings_table.Rows.Add(section_minimalversion);
            #endregion


            #region "Flash Variables" Section
            section_flashvars = new EditorPartTextboxSection("FlashVariables", "Flash Variables", "Enter the Flash Variables. Format: \"name=value\"<newline>");
            section_flashvars.textbox.TextMode = TextBoxMode.MultiLine;
            section_flashvars.textbox.Rows = 6;
            flashmoviesettings_table.Rows.Add(section_flashvars);
            #endregion

            #endregion

            #region "Flash Fallback Settings" collapsable table
            EditorPartCollapsableTable flashfallback_collapsabletable = new EditorPartCollapsableTable(this, "Expand: Flash Fallback Settings", "Collapse: Flash Fallback Settings", "Flash Fallback Settings", "flashfallbacksectiontable", "/_layouts/Macaw.Moss2007.FlashSolution/FlashFallbackSettings.htm");

            EditorPartTable flashfallbacksettings_table = new EditorPartTable();
            flashfallback_collapsabletable.control.Controls.Add(flashfallbacksettings_table);

            #region "Alt Html" Section
            section_althtml = new EditorPartTextboxWithBuilderSection("althtml", "Alternative Html", "Alternative Html");
            section_althtml.textbox.TextMode = TextBoxMode.MultiLine;
            section_althtml.textbox.Rows = 6;
            flashfallbacksettings_table.Rows.Add(section_althtml);
            #endregion

            #region "Express Install" section
            section_expressinstall = new EditorPartAssetUrlSelectorSection("ExpressInstall", "Express Install Url", "Express Install Url");
            section_expressinstall.selector.UseImageAssetPicker = false;
            section_expressinstall.selector.AssetPickerButtonVisible = true;
            section_expressinstall.selector.PickerButtonText = "..";
            flashfallbacksettings_table.Rows.Add(section_expressinstall);

            #endregion

            #endregion
        }

        private Dictionary<string, string> GetTextboxText(string text)
        {
            Dictionary<string, string> flashvars = new Dictionary<string, string>();
            
            StringReader sr = new StringReader(text);
            string line = sr.ReadLine();
            while (line !=null)
            {
                string[] ss = line.Split(new char[] { '=' }, 2);

                if (ss.Length==2)
                {
                    string parameter = ss[0].Trim();
                    string value = ss[1].Trim();
                    flashvars.Add(parameter, value);
                }
                line = sr.ReadLine();
            }
            return flashvars;
        }

        public override bool ApplyChanges()
        {
            EnsureChildControls();
            FlashWebPart flashwebpart = this.WebPartToEdit as FlashWebPart;

            #region Apply FlashVars
            if (flashwebpart != null && section_flashvars != null)
            {
                flashwebpart.FlashAttributes.flashvars = GetTextboxText(section_flashvars.textbox.Text);
            }
            #endregion

            #region Apply ExpressInstall
            if (flashwebpart != null && section_expressinstall != null)
            {
                flashwebpart.XIMovieUrl = section_expressinstall.textbox.Text;
            }
            #endregion

            #region Apply Movie
            if (flashwebpart != null && section_movie != null)
            {
                flashwebpart.MovieUrl = section_movie.textbox.Text;
            }
            #endregion

            #region Section AltHtml
            if (flashwebpart != null && section_althtml != null)
            {
                flashwebpart.AltHtml = section_althtml.textbox.Text;
            }
            #endregion

            #region "Minimal FlashPlayer version requirement" Section
            if (flashwebpart != null && section_minimalversion != null)
            {
                flashwebpart.Version = section_minimalversion.textbox.Text;
            }
            #endregion

            return true;
        }

        public override void SyncChanges()
        {
            EnsureChildControls();
            FlashWebPart flashwebpart = this.WebPartToEdit as FlashWebPart;

            #region Sync FlashVars
            if (flashwebpart != null && section_flashvars != null )
            {
                System.Text.StringBuilder flashvars = new System.Text.StringBuilder();
                foreach (KeyValuePair<string,string> flashvar in flashwebpart.FlashAttributes.flashvars)
                {
                    flashvars.AppendLine(flashvar.Key + "=" + flashvar.Value);
                }
                section_flashvars.textbox.Text = flashvars.ToString();
            }
            #endregion

            #region Sync ExpressInstall
            if (flashwebpart != null && section_expressinstall != null)
            {
                section_expressinstall.selector.AssetUrl = flashwebpart.XIMovieUrl;
                section_expressinstall.textbox.Text = flashwebpart.XIMovieUrl; 
            }
            #endregion

            #region Sync Movie
            if (flashwebpart != null && section_movie != null)
            {
                section_movie.selector.AssetUrl = flashwebpart.MovieUrl;
                section_movie.textbox.Text = flashwebpart.MovieUrl; 
            }
            #endregion

            #region Section AltHtml
            if (flashwebpart != null && section_althtml != null)
            {
                section_althtml.textbox.Text = flashwebpart.AltHtml;
            }
            #endregion

            #region "Minimal FlashPlayer version requirement" Section
            if (flashwebpart != null && section_minimalversion != null)
            {
                section_minimalversion.textbox.Text = flashwebpart.Version;
            }
            #endregion

        }

    }
}
